package com.kitty.game.item.service;

import com.kitty.game.role.model.Role;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.item.model.PointItem;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class PointItemService {
    public int getChongFengSan(Role role) {
        return getPoint(role, PointItem.CHONG_FENG_SAN);
    }

    /**添加宠风散点数*/
    public int addChongFengSan(Role role, int count) {
        return addPointItem(role, count, PointItem.CHONG_FENG_SAN);
    }

    /**
     * 减少宠风散点数，足够数量时才减
     * */
    public boolean subtractChongFengSan(Role role, int count) {
        int subCount = subtractPointItem(role, count, PointItem.CHONG_FENG_SAN, true);
        if (subCount <= 0) {return false;}
        return true;
    }

    /**添加紫气鸿蒙点数*/
    public int addZiQiHongMeng(Role role, int count) {
        return addPointItem(role, count, PointItem.ZI_QI_HONG_MENG);
    }

    /**
     * 减少如意刷道令点数，足够数量时才减
     * */
    public boolean subtractRuYiShuaDaoLing(Role role, int count) {
        int subCount = subtractPointItem(role, count, PointItem.RU_YI_SHUA_DAO_LING, true);
        if (subCount <= 0) {return false;}
        return true;
    }

    public int getDouble(Role role) {
        return getPoint(role, PointItem.DOUBLE);
    }

    /**增加双倍点数*/
    public int addDouble(Role role, int count) {
        return addPointItem(role, count, PointItem.DOUBLE);
    }

    /**
     * 减少双倍点数，足够数量时才减
     * */
    public boolean subtractDouble(Role role, int count) {
        int subCount = subtractPointItem(role, count, PointItem.DOUBLE, true);
        if (subCount <= 0) {return false;}
        return true;
    }

    /**增加神木鼎点数*/
    public int addShenMuDing(Role role, int count) {
        return addPointItem(role, count, PointItem.SHEN_MU_DING);
    }

    /**
     * 减少神木鼎点数，足够数量时才减
     * */
    public boolean subtractShenMuDing(Role role, int count) {
        int subCount = subtractPointItem(role, count, PointItem.SHEN_MU_DING, true);
        if (subCount <= 0) {return false;}
        return true;
    }

    /**
     * 减少急急如律令点数，足够数量时才减
     * */
    public boolean subtractJiJiRuLvLing(Role role, int count) {
        int subCount = subtractPointItem(role, count, PointItem.JI_JI_RU_LV_LING, true);
        if (subCount <= 0) {return false;}
        return true;
    }

    public boolean isOpen(Role role, PointItem pointItem) {
        return getPointStatus(role, pointItem) == 1;
    }

    public byte getPointStatus(Role role, PointItem pointItem) {
        return (byte) role.getPropsStatus().getInt(pointItem.getStatus(), 0);
    }

    public int getPoint(Role role, PointItem pointItem) {
        return role.getPropsStatus().getInt(pointItem.getKey(), 0);
    }

    /**增加点数*/
    public int addPointItem(Role role, int count, PointItem pointItem) {
        if (count <= 0) {return 0;}

        String key = pointItem.getKey();
        NutMap nutMap = role.getPropsStatus();
        int oldCount = nutMap.getInt(key, 0);
        int newCount = oldCount + count;
        if (newCount > pointItem.getMax()) {
            newCount = pointItem.getMax();
        }

        int addCount = newCount - oldCount;
        nutMap.setv(key, nutMap.getInt(key, 0) + addCount);
        role.save();

        FiedValue fiedValue = new FiedValue(pointItem.getFieldId(), role.getPropsStatus().getInt(key, 0));
        List<FiedValue> list = Arrays.asList(fiedValue);
        RespUpdate respUpdate = new RespUpdate(role.getRoleId(), list);
        MessagePusher.pushMessage(role, respUpdate);

        return addCount;
    }

    public int subtractPointItem(Role role, int count, PointItem pointItem) {
        return subtractPointItem(role, count, pointItem, true);
    }

    /**
     * 减少点数
     * @param role
     * @param count
     * @param isEnough 是否是足够数量时才减
     * @return 实际减少的数量
     */
    public int subtractPointItem(Role role, int count, PointItem pointItem, boolean isEnough) {
        if (count <= 0) {return 0;}

        String key = pointItem.getKey();
        int fieldId = pointItem.getFieldId();
        NutMap nutMap = role.getPropsStatus();
        int oldCount = nutMap.getInt(key, 0);
        /**需要足够数量才减，同时数量不足时，返回0*/
        if (isEnough && oldCount < count) {
            return 0;
        }

        int newCount = oldCount - count;
        if (newCount < 0) {
            newCount = 0;
        }

        int subCount = oldCount - newCount;
        if (subCount <= 0) {
            return 0;
        }

        nutMap.setv(key, nutMap.getInt(key, 0) - subCount);
        role.save();

        FiedValue fiedValue = new FiedValue(fieldId, role.getPropsStatus().getInt(key, 0));
        List<FiedValue> list = Arrays.asList(fiedValue);
        RespUpdate respUpdate = new RespUpdate(role.getRoleId(), list);
        MessagePusher.pushMessage(role, respUpdate);

        return subCount;
    }
}
